const express = require("express");
const app = express();
const user = require("./mongodb/user.js");
const rs = require("randomstring")

app.use(express.static("./www"));
app.use(express.urlencoded());

app.post("/addUser", (req,res)=>{

  const {username, password, tel, email, power, isVIP, hobby } = req.body;

  const obj = {
    userId: rs.generate(8),
    username, password, tel, email,
    power: power-0,
    isVIP: !!(isVIP-0),
    hobby: JSON.parse(hobby || "[]")
  }
  
  user.insertMany( obj ).then(()=>{
    res.send("插入成功");
  });

});


app.get("/getuser", (req, res)=>{
  user.find({}, {_id:0, __v:0}).then(data=>{
    res.send( data );
  })
})




app.listen(3000, ()=>{
  console.log("ok");
});